using Digitalmes.Application.Mes.Equipments.Inspections;
using Digitalmes.Domain.Entities.Mes.Equipments;

namespace Digitalmes.Application.Mes.Equipments.InspectManuals.Queries;

/// <summary>
/// 查询所有的手动点检计划，其中包含设备具体信息。
/// </summary>
public sealed record GetAllManualInspectionPlanListQuery() : IRequest<IResult<List<EquipInspectionPlanDto>>>;

internal sealed class GetAllManualInspectionPlanListQueryHandler(IRepository<EquipInspectionPlan> repo)
    : IRequestHandler<GetAllManualInspectionPlanListQuery, IResult<List<EquipInspectionPlanDto>>>
{
    public async Task<IResult<List<EquipInspectionPlanDto>>> Handle(GetAllManualInspectionPlanListQuery request, CancellationToken cancellationToken)
    {
        // 根据点检计划下的手动点检项（同一种设备同一种点检类型只有一个）
        var items = await repo.AsQueryable()
            .Includes(m => m.Machinery, x => x!.Line)
            .Includes(m => m.Machinery, t => t!.Workstation)
            .Where(m => m.InspectType == InspectTypeEnum.Manual && m.Status == EnabledStatusEnum.Enabled)
            .ToListAsync(cancellationToken);
        return Result.From(items.Adapt<List<EquipInspectionPlanDto>>());
    }
}
